/*
 * Copyright (c) 2022, OUXIAOLONG. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.examples.springboot.jpa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.examples.springboot.jpa.domain.Climber;
import com.examples.springboot.jpa.domain.ClimberPhoneNumber;
import com.examples.springboot.jpa.domain.Department;
import com.examples.springboot.jpa.domain.Employee;
import com.examples.springboot.jpa.domain.Mount;
import com.examples.springboot.jpa.repository.ClimberDao;
import com.examples.springboot.jpa.repository.DepartmentRepository;
import com.examples.springboot.jpa.repository.EmployeeRepository;
import com.examples.springboot.jpa.repository.MountDao;

@Component
public class ExampleDataCreator {
  private final MountDao mountDao;
  private final ClimberDao climberDao;

  private final EmployeeRepository employeeDao;
  private final DepartmentRepository departmentDao;

  @Autowired
  public ExampleDataCreator(MountDao mountDao, ClimberDao climberDao,EmployeeRepository employeeDao, DepartmentRepository departmentDao) {
    this.mountDao = mountDao;
    this.climberDao = climberDao;

    this.employeeDao=employeeDao;
    this.departmentDao=departmentDao;
  }

  public void createClimbers() {
    if(climberDao.count()>0)return;
    final Mount everest = mountDao.save(Mount.builder().alias("everest").name("Mount Everest").build());
    final Mount k2 = mountDao.save(Mount.builder().alias("k2").name("K2").build());

    climberDao.save(Climber.builder()
      .name("Alex")
      .phoneNumber(climberDao.savePhoneNumber(ClimberPhoneNumber.builder().number("+86 138 2345 6789").build()))
      .climbedMount(everest)
      .climbedMount(mountDao.save(Mount.builder().alias("aitana").name("Aitana").build()))
      .build());
    climberDao.save(Climber.builder()
      .name("Charles")
      .phoneNumber(climberDao.savePhoneNumber(ClimberPhoneNumber.builder().number("+86 0755 5555 1337").build()))
      .climbedMount(k2)
      .climbedMount(everest)
      .build());
    climberDao.save(Climber.builder()
      .name("Julia")
      .climbedMount(k2)
      .climbedMount(everest)
      .build());
  }

  public void createEmployees() {
    if(employeeDao.count()>0)return;
    final Department sales=departmentDao.save(new Department("sales"));
    final Department advertising=departmentDao.save(new Department("advertising"));

    employeeDao.save(new Employee("Jakab Gipsz","+86 134 3022 9077", sales));
    employeeDao.save(new Employee("Captain Nemo","+86 185 8823 8643", advertising));
    employeeDao.save(new Employee("Bill Burr","+86 153 8618 3493", sales));

    departmentDao.save(new Department("developers"));
    departmentDao.save(new Department("product"));
  }
}
